import axios from "@/utils/axios";
import type { RouteRecordNormalized } from "vue-router";
import { UserState } from "@/store/modules/user/types";

export interface LoginData {
	username: string;
	password: string;
}

export interface LoginRes {
	token: string;
}

// 登录
export const login = (data: LoginData) => {
	return axios.post<LoginRes>({ url: "/login", data });
};

// 安全退出
export const logout = () => {
	return axios.post({ url: "/logout" });
};

// 获取信息
export const getUserInfo = (id: any) => {
	const params = { id: id };
	return axios.get<UserState>({ url: "/user/info", params });
};

// 当前登录用户权限
export function getAuthList() {
	return axios.post<RouteRecordNormalized[]>({ url: "/user/auths" });
}

// 列表
export const getUserList = (params: any) => {
	return axios.get({ url: "user/info", params });
};

// 保存数据
export const saveUserData = (data: any) => {
	if (data?.id) {
		return axios.put({ url: "user/edit", data });
	}
	return axios.post({ url: "user/add", data });
};

// 删除数据
export const deleteUserData = (ids: any) => {
	const data = { ids: ids };
	return axios.delete({ url: "user/delete", data });
};
